/**
 * Created by udi on 2016/12/2.
 */
require(["jv", "vue", "pop", "mui"], function () {

  var Vue = require("vue");
  var computeItem = function () {
    this.数量 = this.number;
    this.名称 = this.name;
    this.分类 = this.category;
    this.单位 = this.Unit;
    this.单价 = this.price;
    this.费用 = this.fee = this.price * this.number;

    if (!this.number) return;
    if (!this.feeFormula) return;

    var feeScript = "var result = (function(){" +
      "var 单价=this.单价,数量=this.数量,名称=this.名称,分类=this.分类,单位=this.单位,费用=this.费用; return "
      + this.feeFormula + "}).call(this); if( !jv.isNull(jv.result) ){ this.fee = result; } ";

    return eval("eval(function(){ " + feeScript + "})").call(this);
  }
  var computeTotal = function () {
    var total = 0;
    for (var i = 0, len = this.prices.length; i < len; i++) {
      var item = this.prices[i];
      computeItem.call(item);
      this[item.name] = item;
      total += item.fee;
    }
    this["总费用"] = this.total = total;
    if (!this.totalFormula) return;
    var feeScript = "var 总费用 = this.total; this.total =  " + this.totalFormula + " ;";

    return eval("eval(function(){ " + feeScript + "})").call(this);
  }

  var bindVue = function (data) {

    var defAdress = data.filter(function (it) {
        return it.isDefault;
      })[0] || data[0] || {};

    data = jv.extend({
      address: "",
      addressId: "",
      serviceType: localStorage.fuwu_name,
      remark: "",
      min_price: 0,
      prices: "[]"
    }, localStorage, defAdress, {addresses: data});

    data.prices = JSON.parse(data.prices);

    computeTotal.call(data);

    var vm = jv.vm = new Vue({
      el: '#app',
      data: data,
      computed: {
        freightFee: function () {
          if (this.total > this.min_price * 100) return 0;
          return this.freight;
        }
      },
      methods: {
        selAddressClick: function (ev) {
          if (this.addresses.length < 2) return;

          // jv.pop({
          //   data: this.addresses,
          //   idKey: "addressId",
          //   nameKey: "address",
          //   current: this.addressId,
          //   changed: function (value) {
          //     vm.$data.addressId = value.id;
          //     vm.$data.address = value.name;
          //   }
          // });

          window.selAddressClick_callback = function (aid, aname) {
            jv.vm.$data.addressId = aid;
            jv.vm.$data.address = aname;
          }
          if (window.android && window.android.openAddress) {
            window.android.openAddress("selAddressClick_callback")
          }
        }
      }
    });

    vm.$nextTick(function () {
      $("#btnOK").click(function () {
        var prices = [];

        $.each(vm.$data.prices, function () {
          if (!this.number) return;
          prices.push({priceId: this.priceId, number: this.number});
        });

        jv.post("/store/order", {
          total: jv.vm.total,
          freight: jv.vm.freightFee,
          storeId: jv.vm.storeId,
          addressId: jv.vm.addressId,
          prices: prices
        }, function (res) {
          if (res.data.status != "waitPay") {
            return alert("状态不是 待支付！");
          }

          var orderId = res.data.id;

          if (window.android && window.android.pay) {
            window.android.pay(orderId);
          }
          else {
            alert("找不到支付接口！");
          }
        });
      });

    })
  }

  jv.post("/user/getAddresses", bindVue);


});
